package qnb.controller;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import qnb._common.easyui.MenuVO;
import qnb._common.easyui.Tree;
import qnb._common.vo.MessageVOFactory;
import qnb.entity.Student;
import qnb.entity.Teacher;
import qnb.service.SysPowerService;
import weixinkeji.vip.jweb.power.ann.JWPIdentifiter;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author huangxin
 * @date 2019/5/20-10:33
 */
@Controller
@RequestMapping(value = "sysPower", produces = "application/json; charset=utf-8")
public class SysPowerController {
    private static final ObjectMapper objectMapper = new ObjectMapper()
            .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

    @Autowired
    private SysPowerService sysPowerService;

    @JWPIdentifiter("admin:teacherPower")
    @RequestMapping("updateTeacherPower")
    @ResponseBody
    public Object updateTeacherPower(@RequestParam("id") String id, @RequestParam("ids") List<Integer> ids) {
        return MessageVOFactory.getUpdateMessage(sysPowerService.updateTeacherPower(id, ids));
    }

    @JWPIdentifiter("admin:teacherPower")
    @RequestMapping("getPowerByTeacherId")
    @ResponseBody
    public List<Tree> readPowerByTeacherId(@RequestParam("id") String id) {
        return sysPowerService.getPowerByTeacherId(id);
    }

    @JWPIdentifiter("admin:studentPower")
    @RequestMapping("updateStudentPower")
    @ResponseBody
    public Object updateStudentPower(@RequestParam("id") String id, @RequestParam("ids") List<Integer> ids) {
        return MessageVOFactory.getUpdateMessage(sysPowerService.updateStudentPower(id, ids));
    }

    @JWPIdentifiter("admin:studentPower")
    @RequestMapping("getPowerByStudentId")
    @ResponseBody
    public List<Tree> readPowerByStudentId(@RequestParam("id") String id) {
        return sysPowerService.getPowerByStudentId(id);
    }

    /**
     * 读取菜单
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("readMenu")
    public List<MenuVO> readAdminMenu(HttpSession session) {
        return sysPowerService.findMenuByUser(session);
    }

    /**
     * @param id
     * @return
     */
    @JWPIdentifiter("admin:role")
    @RequestMapping("readPowerByRoleId")
    @ResponseBody
    public List<Tree> readPowerByRoleId(@RequestParam("id") Integer id) {
        return sysPowerService.getPowerByRoleId(id);
    }

    @JWPIdentifiter({"admin:role", "admin:studentPower", "admin:teacherPower"})
    @ResponseBody
    @RequestMapping("findAllPower")
    public List<Tree> readAllPower(@RequestParam("type") Integer type) {
        return sysPowerService.getAllPower(type);
    }

}
